package com.lxx.aspect;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.lxx.modules.sys.biz.ISysLogBiz;
import com.lxx.modules.sys.entity.SysLog;
import com.lxx.modules.sys.entity.SysUser;
import com.lxx.utils.NetworkUtil;
import com.lxx.utils.SessionUtils;

@Aspect
public class LogAspect {
	
	@Autowired
	private ISysLogBiz iSysLogBiz;
	
	@Pointcut("execution(* com.lxx.modules.*.controller.*.*(..))")
	private void logMethod() {}
	
	@Before("logMethod()")
	public void before(JoinPoint joinPoint) {
//		System.out.println("before invoke..");
//		System.out.println(joinPoint.getClass());
//		System.out.println("目标对象："+joinPoint.getTarget());
//		System.out.println("代理目标对象："+joinPoint.getThis());
//		System.out.println("目标方法签名："+joinPoint.getSignature());
//		System.out.println("目标方法参数数组："+Arrays.toString(joinPoint.getArgs()));
//		System.out.println();
		
		ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = sra.getRequest();
		HttpSession session = request.getSession();
		SysUser user = SessionUtils.sessionUser(session);
		SysLog sysLog = new SysLog();
		sysLog.setClientIp(NetworkUtil.getIpAddress(request));
		sysLog.setContent(String.format("访问url %s", request.getRequestURI()));
		sysLog.setCreateTime(new Date());
		if (user != null) {
			sysLog.setUsername(user.getUsername());
		}
		iSysLogBiz.save(sysLog);
	}

}
